package com.etoak.config;

import com.etoak.common.core.properties.ImageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

/**
 * 配置网关访问静态资源
 */
@Configuration
public class WebFluxConfiguration implements WebFluxConfigurer {

    @Autowired
    private ImageProperties imageProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String mapping = imageProperties.getMapping().endsWith("/") ?
                imageProperties.getMapping() + "**" : imageProperties.getMapping() + "/**";
        String location = "file:" + (imageProperties.getUploadDir().endsWith("/") ?
                imageProperties.getUploadDir() : imageProperties.getUploadDir() + "/");
        registry.addResourceHandler(mapping).addResourceLocations(location);
    }
}
